package convert;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;


public class ECell extends BaseContent
{
	private Cell[] m_cells;
	
	public ECell(Cell cell, EColumn father)
	{
		super("", father);
		m_cells = new Cell[1];
		m_cells[0] = cell;
		m_name = getValue(m_cells[0]);
	}
	
	public ECell(Cell[] cells, EColumn father)
	{
		super("", father);
		m_cells = cells;
		for(Cell c : m_cells)
		{
			m_name += getValue(c);
		}
	}
	
	public Cell[] getRealCells()
	{
		return m_cells;
	}
	
	public static String getValue(Cell cell)
	{
		if(cell == null)
		{
			return "";
		}
		switch (cell.getCellType()) 
		{
		case HSSFCell.CELL_TYPE_BLANK:
			return "";
		case HSSFCell.CELL_TYPE_BOOLEAN:
			return "" + cell.getBooleanCellValue();
		case HSSFCell.CELL_TYPE_NUMERIC:
		{
			double value = cell.getNumericCellValue();
			if(value == (int)value)
			{
				return (int)value + "";
			}
			return value + "";
		}
		case HSSFCell.CELL_TYPE_STRING:
			return formatValue(cell.getStringCellValue());
		case HSSFCell.CELL_TYPE_FORMULA:
			return formatValue(EFormula.getValue(cell));
		default:
			return "";
		}
	}
	
	private static String formatValue(String str)
	{
		str.replace("&lt;", "<");
		str.replace("&gt;", ">");
		return str;
	}
}
